<template>
  <!-- 资讯列表页面 -->
  <div>
    <div class="header">
      <el-form :inline="true" size="mini">
        <el-form-item>
          <el-select clearable v-model="params.status" placeholder="状态">
            <el-option label="未审核" value="未审核"></el-option>
            <el-option label="审核通过" value="审核通过"></el-option>
            <el-option label="审核不通过" value="审核不通过"></el-option>
            <el-option label="推荐" value="推荐"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-input
            clearable
            v-model="params.title"
            placeholder="标题"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-select
            clearable
            v-model="params.categoryId"
            placeholder="请选择栏目"
          >
            <el-option
              v-for="item in cateGory"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="toSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            align="right"
            @click="$router.push({ path: 'Publish' })"
            >发布风采</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="content">
      <el-table
        v-loading="loading"
        :data="article"
        style="width: 100%"
        @row-click="toDetail"
        @click.stop="toDetail"
      >
        <el-table-column type="index" :index="1" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cover" label="封面">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="right">
              <img
                slot="reference"
                fit="contain"
                width="50px"
                :src="scope.row.cover || ''"
                alt=""
              />
              <img
                width="200px"
                fit="contain"
                :src="scope.row.cover || ''"
                alt=""
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="风采">
          <template slot-scope="scope">
            <div class="title">{{ scope.row.title }}</div>
            <div class="name">{{ scope.row.category.name }}</div>
            <div>{{ scope.row.publishTime | fmtDate }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '审核通过'" type="success"
              >审核通过</el-tag
            >
            <el-tag v-else-if="scope.row.status === '审核不通过'" type="danger"
              >审核不通过</el-tag
            >
            <el-tag v-else-if="scope.row.status === '未审核'" type="warning"
              >未审核</el-tag
            >
            <el-tag v-else>推荐</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <a @click.stop="toRecommend(scope.row.id)" class="recommend"
              >推荐</a
            >
            <a @click.stop="toAudit(scope.row)" class="audit">审核</a>
            <a @click.stop="toDelete(scope.row.id)" class="delete">删除</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页查询 -->
    <div class="page">
      <el-pagination
        align="right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 审核模态框 -->
    <el-dialog title="审核风采信息" :visible.sync="visible">
      <el-form :model="form">
        <el-form-item :label="form.title" prop="name"> </el-form-item>
        <el-form-item v-html="form.content" prop="productCategoryId">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="toStop(form.id, form.status)"
          >审核不通过</el-button
        >
        <el-button type="primary" @click="toPass(form.id, form.status)"
          >审核通过</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { cateGory } from "@/api/proCategory";
import { queryArticle, changeStatus, deleteById } from "@/api/article";
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
      },
      loading: false,
      cateGory: [],
      article: [],
      total: 0,
      form: {},
      visible: false,
    };
  },
  computed: {},
  methods: {
    // 去详情页
    toDetail(row) {
      console.log(row, "11");
      this.$router.push({
        path: "Details",
        query: { id: row.id },
      });
    },
    // 查询所有咨询分类
    async getCateGory() {
      let res = await cateGory();
      this.cateGory = res.data;
    },

    // 查询所有咨询
    async getArticle() {
      this.loading = true;
      for (let key in this.params) {
        if (!this.params[key]) {
          delete this.params[key];
        }
      }
      let res = await queryArticle(this.params);
      this.article = res.data.list;
      this.total = res.data.total;
      this.loading = false;
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.page = 1;
      this.getArticle();
    },
    handleCurrentChange(page) {
      this.params.page = page;
      this.getArticle();
    },
    // 查询
    toSearch() {
      this.getArticle();
    },
    // 审核
    toAudit(row) {
      this.visible = true;
      this.form = { ...row };
    },
    // 不通过
    async toStop(id, status) {
      status = "审核不通过";
      var res = await changeStatus({ id, status });
      this.getArticle();
      this.visible = false;
    },
    // 审核通过 toPass
    async toPass(id, status) {
      status = "审核通过";
      var res = await changeStatus({ id, status });
      this.getArticle();
      this.visible = false;
    },
    // 推荐
    toRecommend(id, status) {
      this.$confirm("确定推荐此咨询吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        status = "推荐";
        var res = await changeStatus({ id, status });
        this.getArticle();
        this.$message({
          type: "success",
          message: res.message,
        });
      });
    },
    // 删除
    async toDelete(id) {
      var res = await deleteById({ id });
      this.getArticle();
    },
    // 发布风采
  },
  created() {
    this.getCateGory();
    this.getArticle();
  },
  mounted() {},
};
</script>
<style scoped>
.title {
  font-weight: 600;
  font-size: 13px;
}
.recommend {
  color: #37b9b0;
}
.audit {
  color: orange;
}
.delete {
  color: rgba(255, 0, 0, 0.585);
}
</style>